export default {
  showPage(page) {
    this.loadPage(page, this.page.query)
  },
  sort(sortable) {
    if (sortable.column) {
      const field = sortable.column.columnKey
      const order = sortable.column.order
      const sortby = (order === 'ascending' ? '' : '-') + field
      // re-sort should use last query conditions but not current
      this.page.query.sortby = sortby
    } else {
      this.page.query.sortby = ''
    }
    this.showPage(1)
  },
  loadPage(page, query) {
    let _query = Object.assign({}, query || this.query)
    _query.page = page
    return this.list(page, _query).then(() => {
      // persist last success query
      this.page.query = _query
    })
  },
  reloadPage() {
    const page = this.page.query.page
    const query = this.page.query
    this.loadPage(page, query)
  }
}
